# -*- coding: utf-8 -*-
# @Time    : 2021/8/31 22:52
# @Author  : BrandonGT
# @FileName: 10_PyQt5_QLineEdit_验证器.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class AgeValidator(QValidator):
    def validate(self, p_str, p_int):
        # print(p_str, p_int)
        try:
            if 18 <= int(p_str) and int(p_str) <= 180:
                return (QValidator.Acceptable, p_str, p_int)
            elif 1<= int(p_str) and int(p_str) <= 17:
                return (QValidator.Intermediate, p_str, p_int)
            else:
                return (QValidator.Invalid, p_str, p_int)
        except:
            if len(p_str) == 0:
                return (QValidator.Intermediate, p_str, p_int)
            return (QValidator.Invalid, p_str, p_int)

    def fixup(self, p_str):
        try:
            if int(p_str) < 18:
                return "18"
            return "180"
        except:
            return "18"

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 初始化继承的父类内部的__init__
        self.setWindowTitle("QLineEdit验证器的学习")
        self.resize(300, 300)
        self.setup_ui()

    def setup_ui(self):
        le = QLineEdit(self)
        le.move(100, 100)
        # validator = AgeValidator()
        validator = QIntValidator(self)
        validator.setTop(100)
        validator.setBottom(1)

        le.setValidator(validator)

        le2 = QLineEdit(self)
        le2.move(100, 200)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())